package com.kzd.gtgame.main.mine

import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setTransitionStatusE
import com.kzd.gtgame.jetpack.vm.MineViewModel
import com.kzd.gtgame.main.mine.adapter.MineRecordLotteryAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_mine_record_lottery.*
import kotlinx.android.synthetic.main.include_app_refresh.*

class MineRecordLotteryActivity : OwnerViewModeActivity<MineViewModel>() {

    private val mMineRecordLotteryAdapter = MineRecordLotteryAdapter()

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_record_lottery

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = flMineRecordLotteryBar)
    }

    override fun initView() {

        rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mMineRecordLotteryAdapter

        mSmartRefreshLayout.setOnRefreshListener {
            viewModel.mineMyLottery()
        }
        mSmartRefreshLayout.setOnLoadMoreListener {
            viewModel.mineMyLottery(isLoadMore = true)
        }
    }

    override fun initData() {
        viewModel.mineMyLottery()
    }

    override fun initViewMode() {
        viewModel.myLotteryLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response, mMineRecordLotteryAdapter, mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mMineRecordLotteryAdapter.addData(data)
            } else {
                mMineRecordLotteryAdapter.setNewInstance(data)
            }
        })
    }
}